package org.jxc.web.action;

import java.util.ArrayList;
import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.SalesBackOrder;
import org.jxc.domain.SalesBackOrderProduct;
import org.jxc.domain.SalesOrderProduct;
import org.jxc.service.SalesBackOrderProductService;
import org.jxc.service.SalesBackOrderService;
import org.jxc.service.SalesOrderProductService;
import org.jxc.service.SalesOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class SalesBackOrderAction extends GenericAction<SalesBackOrder> {
	private static final long serialVersionUID = -4665378081525232678L;
	@Autowired
	private SalesOrderService salesOrderService;
	@Autowired
	private SalesOrderProductService salesOrderProductService;
	@Autowired
	private SalesBackOrderService salesBackOrderService;
	@Autowired
	private SalesBackOrderProductService salesBackOrderProductService;

	private SalesBackOrder salesBackOrder;

	private List<SalesBackOrderProduct> itemList;

	private List<Integer> backAmountsList;

	private List<String> salesOrderPoductIdList;

	private List<String> saleBackOrderProductIdList;

	private String salesBackOrderId;

	private Integer auditState;

	private String result;

	public String listPagination() throws Exception {
		page = salesBackOrderService.listPagination(page);
		return SUCCESS;
	}

	public String toSave() throws Exception {
		return SUCCESS;
	}

	/**
	 * 新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		salesBackOrderService.saveSaleBackOrder(salesBackOrder);
		for (int i = 0; i < backAmountsList.size(); i++) {
			SalesBackOrderProduct sbop = new SalesBackOrderProduct();
			sbop.setAmounts(backAmountsList.get(i));
			SalesOrderProduct sop = salesOrderProductService
					.findById(salesOrderPoductIdList.get(i));
			sbop.setSalesOrderProduct(sop);
			sbop.setSalesBackOrder(salesBackOrder);
			salesBackOrderProductService.saveSalesBackOrderProduct(sbop);
		}
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		salesBackOrder = salesBackOrderService.findById(salesBackOrderId);
		itemList = salesBackOrderProductService
				.findAllSalesBackOrderProductBySalesBackOrderId(salesBackOrderId);
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		salesBackOrderService.updateSalesBackOrder(salesBackOrder);
		List<SalesBackOrderProduct> list = salesBackOrderProductService
				.findAllSalesBackOrderProductBySalesBackOrderId(salesBackOrder
						.getId());
		List<String> deleteList = new ArrayList<String>();
		for (SalesBackOrderProduct sbop : list) {
			if (!saleBackOrderProductIdList.contains(sbop.getId())) {
				deleteList.add(sbop.getId());
			}
		}
		salesBackOrderProductService.deleteSalesBackOrderProduct(deleteList);
		for (int i = 0; i < saleBackOrderProductIdList.size(); i++) {
			SalesBackOrderProduct sbop = salesBackOrderProductService
					.findById(saleBackOrderProductIdList.get(i));
			sbop.setAmounts(backAmountsList.get(i));
			salesBackOrderProductService.updateSalesBackOrderProduct(sbop);
		}

		for (int i = saleBackOrderProductIdList.size(); i < backAmountsList
				.size(); i++) {
			SalesBackOrderProduct sbop = new SalesBackOrderProduct();
			sbop.setAmounts(backAmountsList.get(i));
			SalesOrderProduct sop = salesOrderProductService
					.findById(salesOrderPoductIdList.get(i));
			sbop.setSalesOrderProduct(sop);
			sbop.setSalesBackOrder(salesBackOrder);
			salesBackOrderProductService.saveSalesBackOrderProduct(sbop);
		}
		return SUCCESS;
	}

	public String toView() throws Exception {
		salesBackOrder = salesBackOrderService.findById(salesBackOrderId);
		itemList = salesBackOrderProductService
				.findAllSalesBackOrderProductBySalesBackOrderId(salesBackOrderId);
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				salesOrderService.deleteSalesOrder(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String apply() throws Exception {
		salesBackOrder = salesBackOrderService.findById(salesBackOrderId);
		salesBackOrder.setAuditState(JXCString.APPLY);
		salesBackOrderService.updateSalesBackOrder(salesBackOrder);
		return SUCCESS;
	}

	public String audit() throws Exception {
		salesBackOrder = salesBackOrderService.findById(salesBackOrderId);
		salesBackOrder.setAuditState(auditState);
		salesBackOrderService.updateSalesBackOrder(salesBackOrder);
		return SUCCESS;
	}

	public List<SalesBackOrderProduct> getItemList() {
		return itemList;
	}

	public void setItemList(List<SalesBackOrderProduct> itemList) {
		this.itemList = itemList;
	}

	public Integer getAuditState() {
		return auditState;
	}

	public void setAuditState(Integer auditState) {
		this.auditState = auditState;
	}

	public SalesBackOrder getSalesBackOrder() {
		return salesBackOrder;
	}

	public void setSalesBackOrder(SalesBackOrder salesBackOrder) {
		this.salesBackOrder = salesBackOrder;
	}

	public List<Integer> getBackAmountsList() {
		return backAmountsList;
	}

	public void setBackAmountsList(List<Integer> backAmountsList) {
		this.backAmountsList = backAmountsList;
	}

	public List<String> getSalesOrderPoductIdList() {
		return salesOrderPoductIdList;
	}

	public void setSalesOrderPoductIdList(List<String> salesOrderPoductIdList) {
		this.salesOrderPoductIdList = salesOrderPoductIdList;
	}

	public String getSalesBackOrderId() {
		return salesBackOrderId;
	}

	public void setSalesBackOrderId(String salesBackOrderId) {
		this.salesBackOrderId = salesBackOrderId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<String> getSaleBackOrderProductIdList() {
		return saleBackOrderProductIdList;
	}

	public void setSaleBackOrderProductIdList(
			List<String> saleBackOrderProductIdList) {
		this.saleBackOrderProductIdList = saleBackOrderProductIdList;
	}
}
